<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button
          size="mini"
          type="primary"
          @click="formShow = true"
        >添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="roleList">
        <!-- 放置列 -->
        <el-table-column prop="name" align="center" width="200" label="角色">
          <template v-slot="{ row }">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" />
            <span v-else>{{ row.name }}</span>
            <template />
          </template>
        </el-table-column>
        <el-table-column prop="state" align="center" width="200" label="启用">
          <template v-slot="{ row }">
            <el-Switch
              v-if="row.isEdit"
              v-model="row.editRow.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
            <span v-else>{{ row.state === 1 ? "已启用" : "未启用" }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="description" align="center" label="描述">
          <template v-slot="{ row }">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" />
            <span v-else>{{ row.description }}</span>
            <template />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="{ row }">
            <template v-if="row.isEdit">
              <el-button type="text" size="small" @click="hOk(row)">确定</el-button>
              <el-button type="text" size="small" @click="row.isEdit=false">取消</el-button>
            </template>
            <template v-else>
              <el-button type="text" size="small" @click="allotAssign(row.id)">分配权限</el-button>
              <el-button type="text" size="small" @click="hEdit(row)">编辑</el-button>
              <!-- <el-button type="text" size="small">删除</el-button> -->
              <el-popconfirm
                title="你确定要删除吗？"
                @onConfirm="hDel(row.id)"
              >
                <el-button slot="reference" type="text" size="small">删除</el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height: 60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          :current-page="page"
          background
          :total="total"
          :page-size="pagesize"
          layout="prev, pager,sizes, next"
          :page-sizes="[5, 6, 7, 8]"
          @current-change="currentChange"
          @size-change="handleSizeChange"
        />
      </el-row>
    </div>
    <el-dialog title="新增角色" :visible.sync="formShow">
      <el-form ref="roleForm" :model="roleForm" :rules="rules">
        <el-form-item prop="name" label="角色名称">
          <el-input v-model="roleForm.name" style="width: 80%" />
        </el-form-item>
        <el-form-item label="即时配送">
          <el-switch
            v-model="roleForm.state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item prop="description" label="角色描述">
          <el-input
            v-model="roleForm.description"
            :rows="3"
            type="textarea"
            style="width: 80%"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addRole">确 定</el-button>
        <el-button @click="callOff">取 消</el-button>
      </div>
    </el-dialog>
    <assign-permission ref="assign" :cur-id="curId" :show-dialig-perm.sync="showDialigPerm" />
  </div>
</template>

<script>
import { getRoles, addRoles, updataRoles, delRole } from '@/api/role'
import assignPermission from './compontents/assignPermission.vue'
export default {
  name: 'Role',
  components: { assignPermission },
  data() {
    return {
      curId: -1,
      showDialigPerm: false,
      isEdit: true,
      formShow: false,
      roleList: [],
      total: 0,
      pagesize: 5,
      page: 1,
      value: true,
      roleForm: {
        name: '',
        state: 0,
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getRoles()
  },
  methods: {
    // 用户修改一页显示几条数据
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.page = 1
      this.getRoles()
    },
    async getRoles() {
      const res = await getRoles({ page: this.page, pagesize: this.pagesize })
      // console.log(res)
      this.roleList = res.data.rows
      this.roleList.forEach((item) => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', { name: item.name, state: item.state, description: item.description, id: item.id })
      })
      // console.log(this.roleList)
      this.total = res.data.total
    },
    currentChange(page) {
      // console.log(page)
      this.page = page
      this.getRoles()
    },
    // 添加角色
    async addRole() {
      await addRoles(this.roleForm)
      this.$message.success('新增角色成功!')
      this.roleForm = { name: '', state: 0, description: '' } // 清空数据
      this.formShow = false // 关闭弹窗
      this.total = this.total + 1
      this.page = Math.ceil(this.total / this.pagesize)
      this.getRoles() // 重新渲染
    },
    callOff() {
      // 取消
      this.roleForm = { name: '', state: 0, description: '' } // 清空数据
      this.formShow = false // 关闭弹窗
    },
    hEdit(row) { // 点击编辑
      row.isEdit = true
    },
    hOk(row) { // 点击确定
      // console.log(row)
      if (!row.editRow.name || !row.editRow.description) {
        this.$message.error('内容必填')
        return
      }
      updataRoles(row.editRow)
      // console.log(res)
      this.$message.success('更新成功')
      this.getRoles()
    },
    hDel(id) { // 删除
      // console.log(id)
      delRole(id) // 删除
      this.$message.success('删除成功')
      if (this.roleList.length === 1) {
        this.page > 1 && this.page--
      }
      this.getRoles()
    },
    allotAssign(id) {
      this.curId = id
      this.showDialigPerm = true
      this.$nextTick(() => {
        this.$refs.assign.getPermiss()
      })
    }
  }
}
</script>

<style scoped>
.role-operate {
  padding: 10px;
}
</style>
